//公式表达式
var formulaExpressions = function () {
    return [
        //扩展字段
        {
            name: '扩展字段',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 0 && attribute.DescFlexFieldDef;
            },
            expressions: [
                {
                    code: 'SegName',
                    name: '扩展字段名称',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 1 ||
                            attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 3 ||
                            attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 4;
                    },
                    expression: function (attribute) {
                        return 'GetFlexSegNameValue("' + attribute.DescFlexFieldDef.ValueSetDef.Code + '",' + attribute.expression + ')';
                    }
                },
                {
                    code: 'ToDate',
                    name: '转日期',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 2 && (attribute.DescFlexFieldDef.ValueSetDef.ValueType === 3 || attribute.DescFlexFieldDef.ValueSetDef.ValueType === 6);
                    },
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),null,StrToDate(' + attribute.expression + '))';
                    }
                },
                {
                    code: 'ToFloat',
                    name: '转浮点数',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 2 && attribute.DescFlexFieldDef.ValueSetDef.ValueType === 1;
                    },
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),0,StrToFloat(' + attribute.expression + '))';
                    }
                },
                {
                    code: 'ToDecimal',
                    name: '转十进制数',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 2 && attribute.DescFlexFieldDef.ValueSetDef.ValueType === 4;
                    },
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),0,StrToDecimal(' + attribute.expression + '))';
                    }
                },
                {
                    code: 'ToDouble',
                    name: '转双精度浮点数',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 2 && attribute.DescFlexFieldDef.ValueSetDef.ValueType === 4;
                    },
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),0,StrToDouble(' + attribute.expression + '))';
                    }
                },
                {
                    code: 'ToBit',
                    name: '转布尔',
                    filter: function (attribute) {
                        return attribute.DescFlexFieldDef.ValueSetDef.ValidateType === 2 && attribute.DescFlexFieldDef.ValueSetDef.ValueType === 5;
                    },
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),false,IIF(Lcase(' + attribute.expression + ')=="true",true,false))';
                    }
                },
            ]
        },
        //字符串
        {
            name: '字符串',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 0 && attribute.DataTypeKey.FullName === "System.String";
            },
            expressions: [
                {
                    code: 'NoNull',
                    name: '非Null',
                    expression: function (attribute) {
                        return 'IIF(Empty(' + attribute.expression + '),"",' + attribute.expression + ')';
                    }
                },
                {
                    code: 'Len',
                    name: '字符串长度',
                    expression: function (attribute) {
                        return "Len(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Left',
                    name: '取左子串',
                    expression: function (attribute) {
                        return "Left(" + attribute.expression + "," + attribute.Length + ")";
                    }
                },
                {
                    code: 'Right',
                    name: '取右子串',
                    expression: function (attribute) {
                        return "Right(" + attribute.expression + "," + attribute.Length + ")";
                    }
                },
                {
                    code: 'Trim',
                    name: '去掉字符串左右两侧的空格',
                    expression: function (attribute) {
                        return "Trim(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'LTrim',
                    name: '去掉字符串左侧的空格',
                    expression: function (attribute) {
                        return "LTrim(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'RTrim',
                    name: '去掉字符串右侧的空格',
                    expression: function (attribute) {
                        return "RTrim(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Replace',
                    name: '字符串替换',
                    expression: function (attribute) {
                        return 'Replace(' + attribute.fullExpression + ',"old","new")';
                    }
                },
                {
                    code: 'Lcase',
                    name: '字符串变成小写',
                    expression: function (attribute) {
                        return "Lcase(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Ucase',
                    name: '字符串变成大写',
                    expression: function (attribute) {
                        return "Ucase(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Substr',
                    name: '取子串',
                    expression: function (attribute) {
                        return "Substr(" + attribute.expression + ",1,1)";
                    }
                },
                {
                    code: 'Contains',
                    name: '包含',
                    expression: function (attribute) {
                        return 'Contains(' + attribute.expression + ',"")';
                    }
                },
                {
                    code: 'StartsWith',
                    name: '开始于',
                    expression: function (attribute) {
                        return 'StartsWith(' + attribute.expression + ',"")';
                    }
                },
                {
                    code: 'EndsWith',
                    name: '结束于',
                    expression: function (attribute) {
                        return 'EndsWith(' + attribute.expression + ',"")';
                    }
                },
            ]
        },
        //长整数
        {
            name: '长整数',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 0 && attribute.DataTypeKey.FullName === "System.Int64";
            },
            expressions: [
                {
                    code: 'CreatedOn',
                    name: '创建时间',
                    filter: function (attribute) {
                        return attribute.Name === "ID";
                    },
                    expression: function (attribute) {
                        return attribute.metedata.Key.Name.concat(".CreatedOn");
                    }
                },
                {
                    code: 'CreatedBy',
                    name: '创建人',
                    filter: function (attribute) {
                        return attribute.Name === "ID";
                    },
                    expression: function (attribute) {
                        return attribute.metedata.Key.Name.concat(".CreatedBy");
                    }
                },
                {
                    code: 'ModifiedOn',
                    name: '修改时间',
                    filter: function (attribute) {
                        return attribute.Name === "ID";
                    },
                    expression: function (attribute) {
                        return attribute.metedata.Key.Name.concat(".ModifiedOn");
                    }
                },
                {
                    code: 'ModifiedBy',
                    name: '修改人',
                    filter: function (attribute) {
                        return attribute.Name === "ID";
                    },
                    expression: function (attribute) {
                        return attribute.metedata.Key.Name.concat(".ModifiedBy");
                    }
                },
                {
                    code: 'SysVersion',
                    name: 'SysVersion',
                    filter: function (attribute) {
                        return attribute.Name === "ID";
                    },
                    expression: function (attribute) {
                        return attribute.metedata.Key.Name.concat(".SysVersion");
                    }
                },
            ]
        },
        //日期/日期时间
        {
            name: '日期/日期时间',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 0 && (attribute.DataTypeKey.FullName === "System.Date"
                    || attribute.DataTypeKey.FullName === "System.DateTime");
            },
            expressions: [
                {
                    code: 'IsNullDate',
                    name: '空日期',
                    expression: function (attribute) {
                        return 'IsNullDate(' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateToStr',
                    name: '转字符串',
                    expression: function (attribute) {
                        return 'DateToStr(' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateToStrYMD',
                    name: '格式:年-月-日',
                    expression: function (attribute) {
                        return 'DateToStrYMD(' + attribute.expression + ')';
                    }
                },
                {
                    code: 'GetDatePartFromDateTime',
                    name: '获取日期部分',
                    expression: function (attribute) {
                        return 'GetDatePartFromDateTime(' + attribute.expression + ')';
                    }
                },
                {
                    code: 'Year',
                    name: '年',
                    expression: function (attribute) {
                        return "Year(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Month',
                    name: '月',
                    expression: function (attribute) {
                        return "Month(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Day',
                    name: '天',
                    expression: function (attribute) {
                        return "Day(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'DateDiffDay',
                    name: '相隔天数',
                    expression: function (attribute) {
                        return 'DaysBetweenFunc(Today(),' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateDiffMonth',
                    name: '相隔月',
                    expression: function (attribute) {
                        return 'MonthsBetweenFunc(Today(),' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateDiffYear',
                    name: '相隔年',
                    expression: function (attribute) {
                        return 'YearsBetweenFunc(Today(),' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateAddDay',
                    name: '加天数',
                    expression: function (attribute) {
                        return 'dateadd("day",1,' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateAddMonth',
                    name: '加月份',
                    expression: function (attribute) {
                        return 'dateadd("month",1,' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateAddHour',
                    name: '加小时',
                    expression: function (attribute) {
                        return 'dateadd("hour",1,' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateAddMinute',
                    name: '加分钟',
                    expression: function (attribute) {
                        return 'dateadd("minute",1,' + attribute.expression + ')';
                    }
                },
                {
                    code: 'DateAddSecond',
                    name: '加秒',
                    expression: function (attribute) {
                        return 'dateadd("second",1,' + attribute.expression + ')';
                    }
                },
            ]
        },
        //十进制数
        {
            name: '十进制数',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 0 && attribute.DataTypeKey.FullName === "System.Decimal";
            },
            expressions: [
                {
                    code: 'Round2Digits',
                    name: '保留两位小数',
                    expression: function (attribute) {
                        return "Round(" + attribute.expression + ",2)";
                    }
                },
                {
                    code: 'RoundByPrecision',
                    name: '保留精度小数位',
                    filter: function (attribute) {
                        if (attribute.Stereotypes && attribute.Stereotypes.length > 0) {
                            return attribute.Stereotypes.some((stereotype) => stereotype.Name === "MoneyAmountRelationAttribute" && stereotype.TaggedValues.length > 0);
                        }
                        return false;
                    },
                    expression: function (attribute) {
                        let stereotype = attribute.Stereotypes.find((stereotype) => stereotype.Name === "MoneyAmountRelationAttribute");
                        if (!stereotype || !stereotype.TaggedValues || stereotype.TaggedValues.length === 0) return '';
                        let typeTaggedValue = stereotype.TaggedValues.find((taggedValue) => taggedValue.Name === "Type");
                        if (!typeTaggedValue) return '';
                        let toAttributeTaggedValue = stereotype.TaggedValues.find((taggedValue) => taggedValue.Name === "ToAttribute");
                        if (!toAttributeTaggedValue) return '';
                        let precisionExpr = toAttributeTaggedValue.Value;
                        precisionExpr = attribute.metedata.Key.Name.concat(".").concat(precisionExpr);
                        if (typeTaggedValue.Value === "UOM")
                            precisionExpr = precisionExpr.concat(".Round.Precision");
                        if (typeTaggedValue.Value === "Price")
                            precisionExpr = precisionExpr.concat(".PriceRound.Precision");
                        if (typeTaggedValue.Value === "Money")
                            precisionExpr = precisionExpr.concat(".MoneyRound.Precision");
                        return "Round(" + attribute.expression + "," + precisionExpr + ")";
                    }
                },
                {
                    code: 'DecimalToStr',
                    name: '转字符串',
                    expression: function (attribute) {
                        return "DecimalToStr(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Ceiling',
                    name: '取最大整数',
                    expression: function (attribute) {
                        return "Ceiling(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Floor',
                    name: '取最小整数',
                    expression: function (attribute) {
                        return "Floor(" + attribute.expression + ")";
                    }
                },
                {
                    code: 'Abs',
                    name: '取绝对值',
                    expression: function (attribute) {
                        return "Abs(" + attribute.expression + ")";
                    }
                },
            ]
        },
        //实体
        {
            name: '实体',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 1;
            },
            expressions: [
                {
                    code: 'Code',
                    name: '编码',
                    filter: function (attribute) {
                        return !attribute.children || attribute.children.length === 0 || attribute.children.some((childAttribute) => {
                            return childAttribute.Name === "Code";
                        });
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".Code");
                    }
                },
                {
                    code: 'Name',
                    name: '名称',
                    filter: function (attribute) {
                        return !attribute.children || attribute.children.length === 0 || attribute.children.some((childAttribute) => {
                            return childAttribute.Name === "Name";
                        });
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".Name");
                    }
                },
                {
                    code: 'RoundPrecision',
                    name: '精度',
                    filter: function (attribute) {
                        return attribute.DataTypeKey.FullName === "UFIDA.U9.Base.UOM.UOM";
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".Round.Precision");
                    }
                },
                {
                    code: 'PriceRoundPrecision',
                    name: '单价精度',
                    filter: function (attribute) {
                        return attribute.DataTypeKey.FullName === "UFIDA.U9.Base.Currency.Currency";
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".PriceRound.Precision");
                    }
                },
                {
                    code: 'MoneyRoundPrecision',
                    name: '金额精度',
                    filter: function (attribute) {
                        return attribute.DataTypeKey.FullName === "UFIDA.U9.Base.Currency.Currency";
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".MoneyRound.Precision");
                    }
                },
                {
                    code: 'DisplayName',
                    name: '账簿期间名称',
                    filter: function (attribute) {
                        return attribute.DataTypeKey.FullName === "UFIDA.U9.Base.SOB.SOBAccountingPeriod";
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".DisplayName");
                    }
                },
                {
                    code: 'ReportPeriodsDisplay',
                    name: '账簿期间显示名称',
                    filter: function (attribute) {
                        return attribute.DataTypeKey.FullName === "UFIDA.U9.Base.SOB.SOBAccountingPeriod";
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".ReportPeriodsDisplay");
                    }
                },
                {
                    code: 'DocNo',
                    name: '单号',
                    filter: function (attribute) {
                        return !attribute.children || attribute.children.length === 0 || attribute.children.some((childAttribute) => {
                            return childAttribute.Name === "DocNo";
                        });
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".DocNo");
                    }
                },
                {
                    code: 'BusinessDate',
                    name: '日期',
                    filter: function (attribute) {
                        return !attribute.children || attribute.children.length === 0 || attribute.children.some((childAttribute) => {
                            return childAttribute.Name === "BusinessDate";
                        });
                    },
                    expression: function (attribute) {
                        return attribute.expression.concat(".BusinessDate");
                    }
                },
                {
                    code: 'CreatedOn',
                    name: '创建时间',
                    expression: function (attribute) {
                        return attribute.expression.concat(".CreatedOn");
                    }
                },
                {
                    code: 'CreatedBy',
                    name: '创建人',
                    expression: function (attribute) {
                        return attribute.expression.concat(".CreatedBy");
                    }
                },
                {
                    code: 'ModifiedOn',
                    name: '修改时间',
                    expression: function (attribute) {
                        return attribute.expression.concat(".ModifiedOn");
                    }
                },
                {
                    code: 'ModifiedBy',
                    name: '修改人',
                    expression: function (attribute) {
                        return attribute.expression.concat(".ModifiedBy");
                    }
                },
                {
                    code: 'SysVersion',
                    name: 'SysVersion',
                    expression: function (attribute) {
                        return attribute.expression.concat(".SysVersion");
                    }
                },
            ]
        },
        //枚举
        {
            name: '枚举',
            filter: function (attribute) {
                return attribute.DataTypeFlag === 3;
            },
            expressions: [
                {
                    code: 'Value',
                    name: '值',
                    expression: function (attribute) {
                        return attribute.expression + '.Value';
                    }
                },
                {
                    code: 'Code',
                    name: '编码',
                    expression: function (attribute) {
                        return 'F_Sys_GetEnumCode("' + attribute.DataTypeKey.FullName + '",' + attribute.expression + '.Value)';
                    }
                },
                {
                    code: 'Name',
                    name: '名称',
                    expression: function (attribute) {
                        return 'RunFunc("' + attribute.DataTypeKey.FullName + '",' + attribute.expression + '.Value.ToString(),GetCurrentCulture())'
                    }
                },
            ]
        },
    ];
}();